package com.taplinker.client.im.rpc;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.im.domain.ImDataCenter;
import com.taplinker.client.im.rpc.body.Cmd002Rep;
import com.taplinker.client.im.rpc.body.Cmd002Req;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.util.json.JsonUtil;

public class SwitchToken extends SimpleSocketCommand<Cmd002Rep> {

	@Override
	public void yes(Result result) {
		Cmd002Rep rep = (Cmd002Rep) JsonUtil.jsonToObject(((IMMessageResponse) result.object).getMessage().getBody(), Cmd002Rep.class);
		
		ImDataCenter.getInstance().setPriNum(rep.getPriNumbers());
		ImDataCenter.getInstance().setPubNum(rep.getPubNumbers());
		ImDataCenter.getInstance().setCrmNum(rep.getCustomers());
		
		AppConfigFactory.getInstance().getUserInfo().setImToken(rep.getServiceToken());
		
		new Cmd003(null).execute();
		new Cmd004(null).execute();
	}

	@Override
	protected String getCommand() {
		return "002";
	}

	@Override
	protected String getBody() {
		Cmd002Req login = new Cmd002Req();
		login.setOs("Android");
		login.setDeviceToken(AppConfigFactory.getInstance().getUserInfo().getToken());
		return JsonUtil.objectToJsonStr(login);
	}
}
